// Pipe_PipeClampJointRegular.cpp: implementation of the Pipe_PipeClampJointRegular class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Pipe_PipeClampJointRegular.h"
#include "FunctionObject.h"
#include "Joint.h"
#include "JointPortFormer.h"
#include "Part.h"
#include "JointPortSource.h"
#include "PersistentFacade.h"
#include "JointFunction.h"
#include "PipeJointPort.h"
#include "PhsCaculateSetting.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC_CREATE(Pipe_PipeClampJointRegular, JointRegular, RegularSerialNumGenerator(JointFunction::ePIPE, JointFunction::ePIPECLAMP))

Pipe_PipeClampJointRegular::Pipe_PipeClampJointRegular()
{

}

Pipe_PipeClampJointRegular::~Pipe_PipeClampJointRegular()
{

}

BOOL Pipe_PipeClampJointRegular::Execute()
{
	CStringArray *pstrArr = NULL;
	Part* pNextPart = static_cast<Part*>(GetJoint()->GetNext()->GetOwner());

	ASSERT(GetJoint()->GetPrev()->GetRuntimeClass() == RUNTIME_CLASS_EX(PipeJointPort));
	PipeJointPort* pPipePort = static_cast<PipeJointPort*>(GetJoint()->GetPrev());

	JointSystem::JointPortFormer *pFormer = GetJoint()->GetNext()->GetFormer();
//	ASSERT(pFormer->SizeRangeCount() == 3);

	DOUBLE dbFactor = GetCurCaculateSetting().GetPipeDiameterEnlargeCoefficient()/100;
	pstrArr = GetSizeRangeText(pFormer->SizeRangeAt(0));

	CString strRegular;
	CString strTemp;
	strRegular.Format(_T("%g >= %s and %s >= %g"), pPipePort->PipeDiameter()*(1 + dbFactor), 
		pstrArr->GetAt(0), pstrArr->GetAt(0), pPipePort->PipeDiameter()*(1 - dbFactor)); 
	delete pstrArr;
	pstrArr = NULL;

	pstrArr = GetSizeRangeText(pFormer->SizeRangeAt(1));
	strTemp.Format(_T("%s >= %g"), pstrArr->GetAt(0), pPipePort->Press().length());
	strRegular += _T(" and ") + strTemp;
	delete pstrArr;
	pstrArr = NULL;

	pstrArr = GetSizeRangeText(pFormer->SizeRangeAt(2));
	strTemp.Format(_T("%s >= %g"), pstrArr->GetAt(0), pPipePort->Temperature());
	strRegular += _T(" and ") + strTemp;
	delete pstrArr;
	pstrArr = NULL;

	strTemp.Format(_T(" order by abs(dw - %g)"), pPipePort->PipeDiameter());

	strRegular += strTemp;

	GetJoint()->GetNext()->SetForce(pPipePort->Press());
	PartCrudeDataSet* pSet = GetPersistentFacade().GetPartCrudeDataSet();
	pSet->SetDataStandard(pNextPart->GetStandard());
	BOOL bResult = pSet->InstancePart(pNextPart, pNextPart->GetCustomID(), strRegular);
	pSet->Close();

	return bResult;
}